/**
 * 09/dic/2011 - fabio.melillo@gmail.com
 */
package demo.unisannio;

import org.unisannio.remote.FactoryRemote;
import org.unisannio.remote.RemoteBroadcaster;
import org.unisannio.remote.RemoteContentProvider;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ToggleButton;

/**
 * @author fabio
 *
 */
public class ConfigActivity extends Activity {
	
	private RemoteBroadcaster broadRemote;
	private RemoteContentProvider contentRemote;
	private int port=5588;
	
	ToggleButton registrami;
	ToggleButton registraSuperPeer;
	TextView tvOwn;
	TextView tvSuperPeer;
	TextView editSuperPeerAddress;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.config);
		registrami=(ToggleButton) findViewById(R.id.toggleOwn);
		registraSuperPeer=(ToggleButton) findViewById(R.id.toggleSuperpeer);
		tvOwn=(TextView) findViewById(R.id.tvOwnAddress);
		tvSuperPeer=(TextView) findViewById(R.id.tvSuperPeerAddress);
		editSuperPeerAddress=(TextView) findViewById(R.id.etSuperPeerAddress);
		
		
		
		registrami.setOnClickListener(new OnClickListener() {
			

			public void onClick(View v) {
				broadRemote=new FactoryRemote().getRemoteBroadcaster(ConfigActivity.this,port);
				tvOwn.setText(broadRemote.getAddress().toString());
				BroadcastActivity.broadcaster=broadRemote;
				contentRemote=new FactoryRemote().getRemoteContentProvider(ConfigActivity.this,port);
				ContentActivity.contentRemote=contentRemote;
			}
		});
		
		registraSuperPeer.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String superString = editSuperPeerAddress.getText().toString();
				if (superString==null || superString.length()<=0)
					{
					AlertDialog.Builder builder = new AlertDialog.Builder(ConfigActivity.this);
					builder.setMessage("Immetti indirizzo SUPERPEER");
					builder.setCancelable(false);
					builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					               dialog.cancel();
					           }
					       });
					AlertDialog alert = builder.create();
					alert.show();
					registraSuperPeer.setChecked(false);
					}
				else{
					BroadcastActivity.broadcaster.setServer(superString);
					tvSuperPeer.setText(BroadcastActivity.broadcaster.getServerAddress().toString());
				}
				
			}
		});
		
		
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		this.checkConnection();
		
		if (BroadcastActivity.broadcaster!=null){
			registrami.setChecked(true);
			tvOwn.setText(BroadcastActivity.broadcaster.getAddress().toString());
			if (BroadcastActivity.broadcaster.getServerAddress()!=null){
				registraSuperPeer.setChecked(true);
				tvSuperPeer.setText(BroadcastActivity.broadcaster.getServerAddress().toString());
				editSuperPeerAddress.setText(BroadcastActivity.broadcaster.getServerAddress().toString().substring(4));
			}
			else{
				registraSuperPeer.setChecked(false);
				tvSuperPeer.setText("Nessun superpeer registrato");
			}
		}
		else{
			tvOwn.setText("Non ancora registrato");
			tvSuperPeer.setText("Nessun superpeer registrato");
			registrami.setChecked(false);
			registraSuperPeer.setChecked(false);
		}
		
	}
	
	
	private void checkConnection(){
		ConnectivityManager conMgr =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ninfo=conMgr.getActiveNetworkInfo();
		if (ninfo==null || !(ninfo.isConnected())) {
			AlertDialog.Builder builder = new AlertDialog.Builder(ConfigActivity.this);
			builder.setMessage("Controlla di aver attivato una connessione dati valida.");
			builder.setCancelable(false);
			builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
			               dialog.cancel();
			           }
			       });
			AlertDialog alert = builder.create();
			alert.show();
		}
	}
	
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		setResult(RESULT_OK,null);
	}
}
